package com.hzyl.famousreader.view.widget.core.loader;

import com.hzyl.famousreader.view.widget.core.model.Chapter;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 * 书籍独立章节加载器
 */
public class SplitDataLoader extends DataLoader {

  public SplitDataLoader(List<Chapter> chapters, Integer chapterPos, Integer pagePos) {
    this.chapters = chapters;
    this.curChapterPagePos = pagePos == null ? 0 : pagePos;
    this.curChapterPos = chapterPos == null ? 0 : chapterPos;
  }

  @Override
  public BufferedReader openChapter(Chapter chapter) throws IOException {
    if (!chapter.isFileExist()) {
      throw new FileNotFoundException("找不到章节："+ chapter.getTitle());
    }
    return new BufferedReader(new FileReader(chapter.getPath()));
  }
}
